package com.linzm.model;

import lombok.Data;
import lombok.SneakyThrows;

import java.io.InputStream;

/**
 * 把我们用到的请求信息，封装成 Request 对象
 * （根据inputSteam输入流封装）
 *
 * @Date: 2023/11/6 22:35
 * @User linziming
 */
@Data
public class Request {
    /**
     * 请求方法 GET PUT POST DELETE
     */
    private String method;

    /**
     * / ， /index.html
     */
    private String url;

    /**
     * 其他的属性都是通过inputStream解析出来的。
     */
    private InputStream inputStream;

    @SneakyThrows
    public Request(InputStream inputStream) {
        this.inputStream = inputStream;
        //从输入流中获取请求信息
        int count = 0;
        while (count == 0) {
            count = inputStream.available();
        }
        byte[] bytes = new byte[count];
        inputStream.read(bytes);
        String inputsStr = new String(bytes);
        //获取第一行数据
        String firstLineStr = inputsStr.split("\\n")[0];  //GET / HTTP/1.1
        String[] strings = firstLineStr.split(" ");
        this.method = strings[0];
        this.url = strings[1];

        System.out.println("method=====>>" + method);
        System.out.println("url=====>>" + url);
    }
}
